import Vue from 'vue'
import './plugins/axios'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.config.productionTip = false
Vue.use(ElementUI, { size: 'small', zIndex: 3000 });

// 使请求带上凭证信息
axios.defaults.withCredentials = true

//使用钩子函数对路由进行权限跳转
router.beforeEach((to, from, next) => {
  document.title = `${to.meta.title}`;
  let role = window.sessionStorage.getItem('loginUser');
  if (!role && to.meta.permission) {
    next('/');
  }else if(role && to.path.startsWith('/admin')){
    axios.get('/user/getRole',{
      params:{
        user:role
      }
    }).then(function(res){
      if(res.data.data === 'admin'){
        next();
      }else{
        ElementUI.Message({
          message: '您没有操作权限',
          type: 'warning',
          duration:'1500',
          offset:'40'
        });
      }
    })
  }else{
    next();
  }
});

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
